<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="openDialog({type:1,pid:'0'})">添加权限</el-button>
        </div>
        <el-table
          border
          :data="list"
          row-key="id"
        >
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" v-checkBtn="'p-em-add'" type="primary" @click="openDialog({type:2,pid:row.pid})">添加</el-button>
              <el-button type="success" @click="eidt(row.id)">编辑</el-button>
              <el-button type="danger" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <add-permission :id="id" :show-dialog="show" :value="val" @close="closeDialog" @update="getPermissionList" />
      </el-card>
    </div>
  </div>
</template>

<script>
import { getPermissionListAPI, delPermissionAPI } from '@/api'
import { transTree } from '@/utils/transTree'
import AddPermission from './components/add-permission.vue'
export default {
  components: {
    AddPermission
  },
  data() {
    return {
      list: [],
      show: false,
      val: {},
      id: ''
    }
  },
  mounted() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionListAPI()
      this.list = transTree(res.data)
    },
    openDialog(val) {
      this.show = true
      this.val = val
      console.log(val)
    },
    closeDialog() {
      this.show = false
      this.id = ''
    },
    delPermission(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delPermissionAPI(id).catch(err => console.log(err))
        this.$message.success(res.message)
        this.getPermissionList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    eidt(id) {
      this.show = true
      this.id = id
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
  </style>
